package com.example.jh352160.surfingtv.commodity_details;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.commodity_details.customView.Share_page;
import com.example.jh352160.surfingtv.network.item.FavoriteItem;
import com.example.jh352160.surfingtv.network.bean.FavoriteBean;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.FavoriteService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.view.View.VISIBLE;

/**
 * Created by jh352160 on 2016/7/28
 */

public class CommodityDetailsPage extends AppCompatActivity {

    Share_page sharePage;
    Boolean isFavorite = false;
    ImageView closeButton;
    TextView comefrom;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commodity_details_page);
        ImageView shareButton = (ImageView) findViewById(R.id.share_button);
        final ImageView favoriteButton = (ImageView) findViewById(R.id.favority);
        sharePage = (Share_page) findViewById(R.id.share_page);
        closeButton = (ImageView) findViewById(R.id.closeButton);
        comefrom = (TextView) findViewById(R.id.comefrom);

        Intent intent = getIntent();
        String from = intent.getStringExtra("from");
        String detilUrl = intent.getStringExtra("detilUrl");
        boolean isLike = intent.getBooleanExtra("isLike", false);
        final int targetId = intent.getIntExtra("targetId", 0);
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(detilUrl);
        comefrom.setText("来自" + from);
        if (isLike) {
            favoriteButton.setImageResource(R.drawable.commodity_unfavorite);
            isFavorite = true;
        }

        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sharePage.setVisibility(VISIBLE);
                sharePage.startAnim();
            }
        });

        favoriteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //api
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                FavoriteService favoriteService = retrofit.create(FavoriteService.class);
                Call<FavoriteItem> call = favoriteService.getItem(new FavoriteBean(2, targetId));
                call.enqueue(new Callback<FavoriteItem>() {
                    @Override
                    public void onResponse(Call<FavoriteItem> call, Response<FavoriteItem> response) {
                        if (isFavorite) {
                            favoriteButton.setImageResource(R.drawable.commodity_favorite);
                            Toast.makeText(CommodityDetailsPage.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        } else {
                            favoriteButton.setImageResource(R.drawable.commodity_unfavorite);
                            Toast.makeText(CommodityDetailsPage.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                        isFavorite = !isFavorite;
                    }

                    @Override
                    public void onFailure(Call<FavoriteItem> call, Throwable t) {

                    }
                });

            }
        });

        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommodityDetailsPage.this.finish();
            }
        });
    }
}
